<?php
/**
 * Created by PhpStorm.
 * User: UEWARE
 * Date: 2019-7-5
 * Time: 16:02:31
 */

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;

class AuthTokenMiddleware
{
    /**
     * token 验证
     *
     * @param Request $request
     * @param Closure $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $token = $request->header('token');

        if(empty($token)){
            return error(3001,'请登录');
        }

        try{
            $admin = Crypt::decrypt($token);
            $request['admin'] = $admin->toArray();
        }catch (\Exception $e){
            return error(3001,'请登录');
        }

        return $next($request);
    }
}